<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="569px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="工站代号" :rules="[{ required: true, message: '请选中工站代号', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.copyStationId" placeholder="请选中工站代号">
                <el-option v-for="item in state.stationTreeData" :key="item.id" :label="item.stationCode"
                  :value="item.id">
                  <span style="float: left">{{ item.stationCode }}</span>
                  <span style="
                              float: right;
                              color: var(--el-text-color-secondary);
                              font-size: 13px;">
                    {{ item.stationName }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label="工站" :rules="[{ required: true, message: '请选中工站代号', trigger: ['blur', 'change'] }]">
              <template #label>
                <div class="my-flex-y-center">
                  工站<el-tooltip effect="dark" placement="top" :hide-after=0>
                    <template #content>复制多工站</template>
                    <SvgIcon name="ele-InfoFilled" class="ml5" />
                  </el-tooltip>
                </div>
              </template>
              <el-tree-select v-model="form.stationIds" :data="state.stationTreeData" node-key="id"
                :props="{ label: 'stationCode' }" render-after-expand fit-input-width clearable filterable collapse-tags
                collapse-tags-tooltip class="w100" :default-expanded-keys="state.expandRowKeys" multiple
                @current-change="onStationCurrentChange">
                <template #default="{ data }">
                  <span class="my-flex my-flex-between">
                    <span>{{ data.stationCode }}</span>
                    <span class="my-line-1 my-mlr-12" :title="data.lineName">
                      {{ data.lineName }}
                    </span>
                  </span>
                </template>
              </el-tree-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/route-detail-copy/form">
import { reactive, toRefs, getCurrentInstance, ref, } from 'vue'
import { FormulaDetailCopyStatiionInput, WorkStationGetListOutput } from '/@/api/industry/data-contracts'
import { listToTree } from '/@/utils/tree'

import { FormulaDetailApi } from '/@/api/industry/FormulaDetail'
import { WorkStationApi } from '/@/api/industry/WorkStation'
defineProps({
  title: {
    type: String,
    default: '',
  },
})
const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  expandRowKeys: [] as Number[],
  stationTreeData: [] as WorkStationGetListOutput[],
  form: {} as FormulaDetailCopyStatiionInput,

})
const { form } = toRefs(state)

// 打开对话框
const open = async (row: any = {}) => {
  await getStations()
  state.form = {} as FormulaDetailCopyStatiionInput
  state.showDialog = true
}
// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    state.sureLoading = true
    const res = await new FormulaDetailApi().copyDetailForOtherStation(state.form, { showSuccessMessage: true }).catch(() => {
    })
    if (res && res?.success) {
      state.showDialog = false
      state.sureLoading = false
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}

const getStations = async () => {
  const res = await new WorkStationApi().getList({})
  if (res?.success && res.data && res.data.length > 0) {
    state.stationTreeData = listToTree(res.data) as WorkStationGetListOutput[]
  } else {
    state.stationTreeData = []
  }
}
const onStationCurrentChange = (data: WorkStationGetListOutput) => {
  if (data) {
    if (!state.form.copyStationId) {
      state.form.copyStationId = data.id
    }
  }
}
defineExpose({
  open,
})
</script>